package fm.dongman.modellib.local;

import android.content.Context;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

import fm.dongman.animefm.contractlib.contract.FArticleContract;
import fm.dongman.animefm.contractlib.model.FArticleDataModel;

/**
 * Created by Administrator on 2017/10/17.
 */

public class FArticleLocalDataSource implements FArticleContract.IFArticleDataSource {

    private static FArticleLocalDataSource SINGLETON;
    private Context mContext;
    private DataOperationHelper database;

    private FArticleLocalDataSource(Context context) {
        this.mContext=context;
        database=DataOperationHelper.getDataOperationHelper(context);
    }

    public static FArticleLocalDataSource getInstance(Context context)
    {
        if(SINGLETON==null)
        {
            SINGLETON=new FArticleLocalDataSource(context);
        }
        return SINGLETON;
    }
    @Override
    public void getData(@NotNull LoadSourceCallback<? super FArticleDataModel> callback) {
        FArticleDataModel farticle=database.downFarticle();

        Log.e("----------------------漫圈本地数据","    "+farticle.getNewArticles().size()+farticle.getDynamicArticles().size()+farticle.getHotArticles().size());

        if (farticle.getDynamicArticles().size() > 0)
        {
            callback.onDataLoaded(farticle);
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(FArticleDataModel dataModel) {
        database.addFArticleHot(dataModel.getHotArticles());
        database.addFArticleNew(dataModel.getNewArticles());
        database.addFArticleBanner(dataModel.getDynamicArticles());
    }

    @Override
    public void refreshData() {

    }

    @Override
    public void offLine() {

    }
}
